package javaTest05.exercise02.sever;

import com.alibaba.fastjson.JSON;
import javaTest05.exercise02.dto.MessageDto;
import javaTest05.exercise02.dto.MsgType;
import javaTest05.utils.LogUtils;
import javaTest05.utils.SocketUtils;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;

public class ClientSocketHandler implements Runnable {

    private Socket client;
    private DataInputStream clientGet;
    private DataOutputStream clientSend;
    private ArrayList<ClientSocketHandler> pool;

    private String hostName;

    public ClientSocketHandler(Socket client, ArrayList<ClientSocketHandler> pool) throws IOException {
        this.client = client;
        this.pool = pool;
        this.clientGet = new DataInputStream(client.getInputStream());
        this.clientSend = new DataOutputStream(client.getOutputStream());
    }

    @Override
    public void run() {
        try {

            while (true) {
                String msg = clientGet.readUTF();//获取传输的数据

                MessageDto messageDto = JSON.parseObject(msg, MessageDto.class);//进行反序列化

                String msgReceiver = null;
                boolean isNeedToForward = true;

                switch (messageDto.getType()) {
                    case Register: {
                        hostName = messageDto.getMsg();

                        isNeedToForward = false;
                    }
                    break;
                    case Private_Msg: {
                        msgReceiver = messageDto.getTo();
                    }
                    break;
                    case Group_Msg: {
                    }
                    break;
                }

                LogUtils.info(
                        String.format("ip地址: %s %s 发送信息: %s 接收对象: %s 类型: %s",
                                client.getRemoteSocketAddress(),
                                hostName,
                                messageDto.getMsg(),
                                messageDto.getTo() == null ? "群聊":messageDto.getTo(),
                                messageDto.getType()
                        ));//日志输出

                if (isNeedToForward)
                    messageForward(messageDto.getFrom(), messageDto.getMsg(), msgReceiver);//消息转发
            }

        } catch (Exception e) {
            pool.remove(this);
            LogUtils.error("客户端:" + client.getRemoteSocketAddress() + " 链接已失效,连接已断开");
        } finally {
            SocketUtils.close(clientGet, clientSend);
        }
    }

    public void messageForward(String from, String msg, String to) throws IOException {//消息转发
        for (ClientSocketHandler other : pool) {
            if (other != this) {

                if (other.getHostName().equals(to) || to == null) {

                    MessageDto messageDto = new MessageDto(from, to, MsgType.Group_Msg);
                    messageDto.setMsg(msg);
                    messageDto.setStatus(true);
                    other.sendMsg(JSON.toJSONString(messageDto));

                }
            }
        }
    }

    public void sendMsg(String msg) throws IOException {//发送消息给客户端
        clientSend.writeUTF(msg);
        clientSend.flush();
    }

    public String getHostName() {
        return this.hostName;
    }

}
